package com.ruoyi.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.domain.entity.ProjectImplement;
import com.ruoyi.project.domain.entity.ProjectMatching;
import com.ruoyi.project.mapper.ProjectImplementMapper;
import com.ruoyi.project.service.IProjectImplementService;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ProjectMatchingMapper;
import com.ruoyi.project.service.IProjectMatchingService;

import javax.annotation.Resource;

/**
 * 配套施工Service业务层处理
 * 
 * @author zzy
 * @date 2025-10-13
 */
@Service
public class ProjectMatchingServiceImpl  extends ServiceImpl<ProjectMatchingMapper, ProjectMatching>   implements IProjectMatchingService
{
    @Autowired
    private ProjectMatchingMapper projectMatchingMapper;

    @Resource
    private ISysDictDataService dictService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询配套施工
     * 
     * @param id 配套施工主键
     * @return 配套施工
     */
    @Override
    public ProjectMatching selectProjectMatchingById(Long id)
    {
        return projectMatchingMapper.selectById(id);
    }

    /**
     * 查询配套施工列表
     * 
     * @param matching 配套施工
     * @return 配套施工
     */
    @Override
    public List<ProjectMatching> selectProjectMatchingList(ProjectMatching matching)
    {
        LambdaQueryWrapper<ProjectMatching> queryWrapper = new LambdaQueryWrapper<ProjectMatching>();
        queryWrapper.eq(ProjectMatching::getProjectNo, matching.getProjectNo());
        queryWrapper.orderByDesc(ProjectMatching::getAmount);
        return projectMatchingMapper.selectList(queryWrapper);
    }

    /**
     * 新增配套施工
     * 
     * @param projectMatching 配套施工
     * @return 结果
     */
    @Override
    public int insertProjectMatching(ProjectMatching projectMatching)
    {
        if (StringUtils.isEmpty(projectMatching.getProjectNo())){
            throw new RuntimeException("新增出错,请联系管理员！");
        }
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        projectMatching.setOperator(nickName);
        projectMatching.setCreateTime(DateUtils.getNowDate());
        return projectMatchingMapper.insert(projectMatching);
    }

    /**
     * 修改配套施工
     * 
     * @param projectMatching 配套施工
     * @return 结果
     */
    @Override
    public int updateProjectMatching(ProjectMatching projectMatching)
    {
        projectMatching.setUpdateTime(DateUtils.getNowDate());
        String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        projectMatching.setOperator(nickName);
        return projectMatchingMapper.updateById(projectMatching);
    }

    public void checkChange(ProjectMatching matching){
        //判断有无核算信息
        String projectNo = matching.getProjectNo();

    }

    /**
     * 批量删除配套施工
     * 
     * @param ids 需要删除的配套施工主键
     * @return 结果
     */
    @Override
    public int deleteProjectMatchingByIds(Long[] ids)
    {
        return projectMatchingMapper.deleteProjectMatchingByIds(ids);
    }

    /**
     * 删除配套施工信息
     * 
     * @param id 配套施工主键
     * @return 结果
     */
    @Override
    public int deleteProjectMatchingById(Long id)
    {
        return projectMatchingMapper.deleteById(id);
    }

    @Override
    public void initMatching(String projectNo) {
        if (StringUtils.isEmpty(projectNo)) {
            throw new RuntimeException("后端服务出错,请联系管理员");
        }

        SysDictData reqVo = new SysDictData();
        reqVo.setDictType("matching_type");
        List<SysDictData> dictDataList = dictDataService.selectDictDataList(reqVo);
        List<ProjectMatching> addList = new ArrayList<>();

        for (SysDictData dictData : dictDataList) {
            //字典值
            String dictValue = dictData.getDictValue();
            //名称
            String dictLabel = dictData.getDictLabel();
            //备注(单位)
            String remark = dictData.getRemark() != null ? dictData.getRemark() : "-";

            ProjectMatching matching = new ProjectMatching();
            matching.setProjectNo(projectNo);
            matching.setName(dictLabel);
            matching.setMatchingType(dictValue);
            matching.setUnit(remark);

            //查询过滤掉重复的
            LambdaQueryWrapper<ProjectMatching> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProjectMatching::getProjectNo, matching.getProjectNo());
            queryWrapper.eq(ProjectMatching::getMatchingType, dictValue);
            List<ProjectMatching> listMatchings = projectMatchingMapper.selectList(queryWrapper);
            if (listMatchings != null && listMatchings.size() > 0  ) {
                continue;
            }else {
                projectMatchingMapper.insert(matching);
            }

        }

    }
}
